package com.nut.esdemo.util;

import com.nut.esdemo.common.HttpResult;
import com.nut.esdemo.entity.Book;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.http.converter.json.GsonBuilderUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HtmlParseUtil {

    public static final String ENCODING = "UTF-8";

    public static List<Book> getJdBook(String key) throws Exception {

//        String url = "https://search.jd.com/Search?keyword=" + key + "&enc=utf-8&wq=java&pvid=536e7de9297b415b8dce53e31453ea41";
        String url = "https://search.jd.com/Search?keyword=" + key;

        HttpResult httpResult = doGet(url);
        if (httpResult == null) {
            System.out.println("httpresult=null");
            return null;
        }
//        System.out.println(httpResult.getContent());
        Document document = Jsoup.parse(httpResult.getContent());

        Element element = document.getElementById("J_goodsList");

        System.out.println(element.html());

        Elements elements = element.getElementsByTag("li");

        ArrayList<Book> books = new ArrayList<>(elements.size());
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();

            System.out.println("img=" + img);
            System.out.println("price=" + price);
            System.out.println("title=" + title);

            books.add(new Book(title, img, price));
        }
        return books;
    }

    public static void main(String[] args) throws Exception {

        List<Book> java = HtmlParseUtil.getJdBook("java");
        java.forEach(System.out::println);
        System.out.println(java);


        String url = "https://search.jd.com/Search?keyword=java&enc=utf-8&wq=java&pvid=536e7de9297b415b8dce53e31453ea41";

        HttpResult httpResult = doGet(url);
        if (httpResult == null) {
            System.out.println("httpresult=null");
            return;
        }
//        System.out.println(httpResult.getContent());
        Document document = Jsoup.parse(httpResult.getContent());

        Element element = document.getElementById("J_goodsList");

        System.out.println(element.html());

        Elements elements = element.getElementsByTag("li");

        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            String price = el.getElementsByClass("p-price").eq(0).text();
            String name = el.getElementsByClass("p-name").eq(0).text();

            System.out.println("img=" + img);
            System.out.println("price=" + price);
            System.out.println("name=" + name);
        }


    }

    private static final HttpResult doGet(String url) throws IOException {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setDoOutput(true);
            conn.setDoInput(true);

//            user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36

            conn.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36");
//            conn.addRequestProperty("Content-Type", "application/json;charset=" + ENCODING);
//            if (token != null) {
//                conn.addRequestProperty("token", token);
//            }
//            conn.getOutputStream().write(jsonReq.getBytes(ENCODING));

            int respCode = conn.getResponseCode();
            String resp = null;
            if (HttpURLConnection.HTTP_OK == respCode) {
                resp = IoUtils.toString(conn.getInputStream(), ENCODING);
            } else {
                resp = IoUtils.toString(conn.getErrorStream(), ENCODING);
            }
            return new HttpResult(respCode, conn.getHeaderFields(), resp);
        } finally {
            IoUtils.closeQuietly(conn);
        }
    }
}
